/*
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

@title: Shapes_That_Go
@description: A game where you control a player and try to reach the end of the level while avoiding obstacles by jumping and reversing gravity.
@author: Parritz
@tags: []
@addedOn: 2024-11-23
*/

const playerChar = "p"
const squareChar = "s"
const pogChar = "o"
const bounceChar = "b"
const spikeChar = "i"
const upsideDownSpikeChar = "u"
const levelX = 12
const levelY = 9
let gameStarted = false
let gameEnded = false

// Define sprites
setLegend(
  [playerChar, bitmap`
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333`],
  [squareChar, bitmap`
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555`],
  [pogChar, bitmap`
....00000000....
...0111111110...
..011111111110..
.01111111111110.
0111111555111110
0111115111511110
0111115111511111
0111115111511110
0111115555111110
0111115111111110
0111115111111110
0111115111111110
.01111111111110.
..011111111110..
...0111111110...
....00000000....`],
  [bounceChar, bitmap`
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444`],
  [spikeChar, bitmap`
................
.......66.......
......6666......
......6666......
.....666666.....
.....666666.....
....66666666....
....66666666....
...6666666666...
...6666666666...
..666666666666..
..666666666666..
.66666666666666.
.66666666666666.
6666666666666666
6666666666666666`],
  [upsideDownSpikeChar, bitmap`
6666666666666666
6666666666666666
.66666666666666.
.66666666666666.
..666666666666..
..666666666666..
...6666666666...
...6666666666...
....66666666....
....66666666....
.....666666.....
.....666666.....
......6666......
......6666......
.......66.......
................`],
)

/// Levels

const startingChunk = [
    [0,0,0,0,0,0,0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,7,7,7,7,7,7,7,7],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
];

const chunks = [chunk2,chunk2,chunk3,chunk4,chunk5,chunk6,chunk7,chunk8,chunk9,chunk10, chunk11,chunk12,chunk13, chunk14, chunk15, chunk16, chunk17, chunk18, chunk19, chunk20, bonus];

let level = 0
const levels = {
  game: map`
............
.p..........
............
............
............
............
............
............
............
............`,
  gameOver: map`
ppppppppppp
ppppppppppp
ppppppppppp
ppppppppppp
ppppppppppp
ppppppppppp
ppppppppppp
ppppppppppp
ppppppppppp`
}

setMap(levels.game)

// Get the player then set some values
let player = getFirst(playerChar)
let isJumping = false
let jumpingStage = 0
let gravityFlipped = false
let playerYVelocity = 0

let score = 0
function setScore(score) {
  clearText()
  addText("Score: " + score, {
    x: 1,
    y: 1,
    color: color`0`
  })
}

function scrollLevel(player) {
  const obstacles = [
    ...getAll(squareChar),
    ...getAll(spikeChar),
    ...getAll(upsideDownSpikeChar)
  ]
  const bounces = getAll(bounceChar)
  const pogs = getAll(pogChar);

  // Handle obstacles
  for (const obstacle of obstacles) {
    const nextXPosition = obstacle.x - 1;

    // Handle collision
    // If the next position of the obstacle is the player, then end the game
    if (nextXPosition == player.x && obstacle.y == player.y) {
      endGame()
    } else {
      if (obstacle.x == 0) {
        clearTile(0, obstacle.y);
      }
    }

    obstacle.x = nextXPosition;
  }

  // Handle pogs
  for (const pog of pogs) {
    const nextXPosition = pog.x - 1;

    // Handle collision
    if (nextXPosition == player.x && pog.y == player.y) {
      score += 1000
      clearTile(pog.x, pog.y)
    } else {
      if (pog.x == 0) {
        clearTile(0, pog.y)
      }
    }
    pog.x = nextXPosition
  }

  for (const bounce of bounces) {
    const nextXPosition = bounce.x - 1;

    // Handle collision
    let isAffected = (gravityFlipped ? bounce.y + 1 : bounce.y - 1) == player.y;
    if (nextXPosition == player.x && isAffected) {
      playerYVelocity = 6
    } else {
      if (bounce.x == 0) {
        clearTile(0, bounce.y)
      }
    }

    bounce.x = nextXPosition
  }
}

function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function getRandomChunk() {
  const randomNumber = getRandomInt(0, chunks.length);
  return chunks[randomNumber];
}

let chunkPassed = true
let currentChunk = null
let streamedChunkX = 0;
let streamedChunkY = 0;

function loadChunk(chunk, isPartialStream) {
  if (!chunk) {
    return;
  }
  
  if (chunk[0].length > levelX) {
    // Stream the chunk as it overflows on the x-axis
    chunkPassed = false;

    if (isPartialStream) {
      // Load as much as possible from left to right
      for (let chunkY = 0; chunkY < chunk.length; chunkY++) {
        if (chunkY > levelY) {
          continue;
        }
        
        for (let chunkX = 0; chunkX < Math.min(levelX, chunk[0].length); chunkX++) {
          if (chunk[chunkY][chunkX] == 1) {
            addSprite(chunkX, chunkY, squareChar);
          }
        }
      }
    } else {
      // Normal streaming logic
      for (let chunkY = 0; chunkY < chunk.length; chunkY++) {
        if (chunkY > levelY) {
          continue;
        }
        
        if (!gameEnded) {
          switch (chunk[chunkY][streamedChunkX]) {
            case 1: {
              addSprite(levelX - 1, chunkY, squareChar);
              break;
            }
            case 2: {
              addSprite(levelX - 1, chunkY, spikeChar);
              break;
            }
            case 3: {
              addSprite(levelX - 1, chunkY, upsideDownSpikeChar);
              break;
            }
            case 4: {
              addSprite(levelX - 1, chunkY, bounceChar);
              break;
            }
            case 7: {
              addSprite(levelX - 1, chunkY, pogChar);
              break;
            }
          }
          
        }
      }

      if (streamedChunkX == chunk[0].length - 1) {
        streamedChunkX = 0;
        chunkPassed = true;
      } else {
        streamedChunkX += 1;
      }
    }
  } else {
    // Level does not overflow, we're safe to load the full thing
    for (let y = 0; y < chunk.length; y++) {
      for (let x = 0; x < chunk[y].length; x++) {
        // Check if the value is non-zero and within the x limit
        if (chunk[y][x] == 1 && x < levelX) {
          addSprite(x, y, squareChar);
        }
      }
    }
  }
}

function isPlayerOnGround(player, isGravityFlipped) {
  const yPosToCheck = isGravityFlipped ? player.y - 1 : player.y + 1;
  const tiles = getTile(player.x, yPosToCheck);
  if (tiles != "") {
    switch (tiles[0]._type) {
      case "s": {
        return true;
      }
      case "b": {
        return true;
      }
      case "i": {
        return false;
      }
      case "u": {
        return false;
      }
    }
  }
  return false;
}

// Set default score
setScore(0)

addText("Up to start", {
  x: 1,
  y: 3,
  color: color`0`
})

/// Game loop
function beginGameLoop() {
  // Load map
  setMap(levels.game)
  
  // Get the player then set some values
  player = getFirst(playerChar);
  isJumping = false;
  gravityFlipped = false;
  score = 0;
  jumpingStage = 0;
  playerYVelocity = 0;
  gameStarted = true;
  gameEnded = false;

  // Reset chunk data
  chunkPassed = true
  currentChunk = null
  streamedChunkX = 0;
  streamedChunkY = 0;

  // Load starting level
  loadChunk(startingChunk, true)
  currentChunk = startingChunk
  
  const gameInterval = setInterval(() => {
    if (player) {
      // Apply gravity or jumping logic
      if (!isPlayerOnGround(player, gravityFlipped) && !isJumping) {
        // Gravity pulls the player down
        player.y += gravityFlipped ? -1 : 1;
      } else if (isJumping) {
        // Jumping logic with stages for smoother ascent and descent
        if (jumpingStage == 0) {
          player.y -= gravityFlipped ? -2 : 2; // Strong initial jump
        } else if (jumpingStage == 1) {
          player.y -= gravityFlipped ? -1 : 1; // Mid-air slowdown
        } else if (jumpingStage == 2) {
          player.y -= gravityFlipped ? 0 : 0; // Pause briefly at the top
        }
        
        jumpingStage += 1;
        
        // End jump after 3 stages
        if (jumpingStage >= 3) {
          isJumping = false;
          jumpingStage = 0;
        }
      }

      if (player.y == levelY || player.y == 0) {
        endGame()
      }
    }

    // Handle player y velocity
    if (playerYVelocity > 0) {
      player.y -= gravityFlipped ? -2 : 2;
      playerYVelocity -= playerYVelocity >= 2 ? 2 : 1;
    }
  
    if (gameStarted) {
       // Load next chunk
      const chunk = chunkPassed ? getRandomChunk() : currentChunk;
      if (chunkPassed) {
        currentChunk = chunk
      }
      
      scrollLevel(player)
      loadChunk(chunk)
      
      // Add to the player's score
      score += 100
      setScore(score)
    }

    if (gameEnded) {
      // End the game loop
      clearInterval(gameInterval);

      // Game over screen text
      addText("Game Over", {
        x: 5,
        y: 6,
        color: color`0`
      });

      addText("Up to reset", {
        x: 4,
        y: 8,
        color: color`0`
      });
    }
  }, 65)

  return gameInterval
}

// Create the game loop
let gameInterval = beginGameLoop()

// Handle game end
function endGame() {
  gameEnded = true;
  gameStarted = false;
  setMap(levels.gameOver);
}

// Handle game restarting
function restartGame() {
  gameInterval = beginGameLoop()
}

/// User input

// Handle game starting
onInput("w", () => {
  if (!gameStarted && !gameEnded) {
    // Begin the game
    gameStarted = true;
    clearText()
  } else if (gameEnded) {
    // Restart the game
    restartGame()
  }
})

// Handle jumping
onInput("s", () => {
  if (!isJumping && isPlayerOnGround(player, gravityFlipped)) {
    isJumping = true
  }
})

// Handle gravity flipping
onInput("k", () => {
  if (isPlayerOnGround(player, gravityFlipped)) {
    gravityFlipped = !gravityFlipped;
  }
})